from backend.backend import Backend
from workflow.task import Task
from workflow.result import Result 
from workflow.flowData import Status
from unique import UNIQUE;

class FoodlerBackend(Backend):   
    def __init__(self): 
        Backend.__init__(self, "Foodler"+UNIQUE)

    def wire(self):
        self.register_result_listener("Foodler", "PlaceOrder", self.process_order)
        self.register_result_listener("Restaurant", "ConfirmOrder", self.confirm_order)
        self.register_result_listener("Deliver", "DeliverOrder", self.deliver_order)

    def process_order(self, results):
        for result in results:  
            task = Task.construct_from_result(result, "Restaurant", "ConfirmOrder");
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

    def confirm_order(self, results):
        for result in results:
            task = Task.construct_from_result(result, "Deliver", "DeliverOrder"); 
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);
            
    def deliver_order(self, results):
        for result in results:
            task = Task.construct_from_result(result, "Deliver", "GetPayment"); 
            self.workflow.add(task);
            self.workflow.update_status(result, Status.COMPLETE);

if __name__ == '__main__':
    backend = FoodlerBackend()